﻿import mx.utils.Delegate;

import com.gamemeal.navigation.INavButtonElement;
import com.gamemeal.navigation.Navigation;
import com.mixmedia.mx.AbstractEventDispatcher;

import events.NavButtonEvent;
import events.NavigationEvent;
/**
 * @author Colin
 */
class com.gamemeal.navigation.NavButton extends AbstractEventDispatcher{
	private var _sectionId:String;
	public var target:String;
	public var _isHighlight:Boolean = false;

	public function NavButton(hitarea:Button,sectionId:String){
		_sectionId = (sectionId!=null)?sectionId:hitarea._name.substr(6);
		hitarea.onRollOver = Delegate.create(this,over);
		hitarea.onRollOut = Delegate.create(this,out);
		hitarea.onRelease = Delegate.create(this,click);
		hitarea.onDragOver = hitarea.onRollOver;
		hitarea.onDragOut = hitarea.onRollOut;
		hitarea.onReleaseOutside = hitarea.onRelease;

		Navigation.instance().addEventListener(NavigationEvent.EVENTCHANGE,Delegate.create(this,highlightButton));
		register();
	}

	public function get sectionId():String{
		return _sectionId;
	}

	public function set isHighlight(bln:Boolean):Void{
		if(bln!=_isHighlight){
			_isHighlight = bln;
			reset();
		}
	}

	public function get isHighlight():Boolean{
		return _isHighlight;
	}

	public function addElements(element:INavButtonElement):Void{
//		if(element.onClick==null)trace('Element event handler not found. This may fix by put the NavButton.addElements in onLoad function');
		this.addEventListener(NavButtonEvent.EVENTROLLOVER,Delegate.create(element,element.onOver));
		this.addEventListener(NavButtonEvent.EVENTROLLOUT,Delegate.create(element,element.onOut));
		this.addEventListener(NavButtonEvent.EVENTRELEASE,Delegate.create(element,element.onClick));
		this.addEventListener(NavButtonEvent.EVENTRESET,Delegate.create(element,element.onReset));
	}

	private function out():Void{
		dispatchEvent(new NavButtonEvent(this,NavButtonEvent.EVENTROLLOUT,sectionId,isHighlight));
	}

	private function over():Void{
		dispatchEvent(new NavButtonEvent(this,NavButtonEvent.EVENTROLLOVER,sectionId,isHighlight));
	}
	
	private function reset():Void{
		dispatchEvent(new NavButtonEvent(this,NavButtonEvent.EVENTRESET,sectionId,isHighlight));
	}
	
	private function click(){
		var nav:Navigation = Navigation.instance();
		nav.changeSection(sectionId,target);
		dispatchEvent(new NavButtonEvent(this,NavButtonEvent.EVENTRELEASE,sectionId,isHighlight,target));
	}

	static private var navBtnReference : Array;

	private function register(){
		if(navBtnReference==null)navBtnReference = new Array();
		if(navBtnReference[sectionId]==null){
			navBtnReference[sectionId] = new Array();
		}
		navBtnReference[sectionId].push(this);
	}
	
	private function highlightButton(evt:NavigationEvent):Void{//eg. 1_2_4
		var callerNavID:String = evt.value;
		var i,j,name,matchName;
		//reset all navbtn status
		for(name in navBtnReference){
			for(i=0;i<navBtnReference[name].length;i++){
				navBtnReference[name][i].isHighlight = false;
			}
		}

		//find which button need to be highlight
		//callerNavID = 1_2_4
		//matchName = 1 , matchName=1_2, matchName = 1_2_4
		for(i=0;i<callerNavID.split("_").length;i++){
			matchName = callerNavID.split("_",i+1).join("_");
			if(navBtnReference[matchName]!=null){
				for(j=0;j<navBtnReference[matchName].length;j++){
					NavButton(navBtnReference[matchName][j]).isHighlight = true;
				}
			}
		}

		for(name in navBtnReference){
			for(i=0;i<navBtnReference[name].length;i++){
				navBtnReference[name][i].reset();
			}
		}
	};
}